import librosa
import librosa.display
import matplotlib.pyplot as plt
import numpy as np

if "__main__" == __name__:
    filepath = r"C:\Users\Administrator\Desktop\heart_sound\four_audios\AS_New\New_AS_001.wav"
    signal, sr = librosa.load(path=filepath, sr=16000)
    N_FFT = 512
    N_MELS = 80
    N_MFCC = 13
    print(signal.shape)
    mel_spec = librosa.feature.melspectrogram(y=signal,
                                              sr=sr,
                                              n_fft=N_FFT,
                                              hop_length=sr // 100,
                                              win_length=sr // 40,
                                              n_mels=N_MELS)
    print(mel_spec.shape)
    mfcc = librosa.feature.mfcc(S=librosa.power_to_db(mel_spec), n_mfcc=N_MFCC)
    print(mfcc.shape)
    delta_mfcc = librosa.feature.delta(data=mfcc)
    delta2_mfcc = librosa.feature.delta(data=mfcc, order=2)
    mfcc = np.concatenate([mfcc, delta_mfcc, delta2_mfcc], axis=0)
    print(mfcc.shape)
    librosa.display.specshow(data=mfcc,
                             sr=sr,
                             n_fft=N_FFT,
                             hop_length=sr // 100,
                             win_length=sr // 40,
                             x_axis="s")
    plt.colorbar(format="%d")

    plt.show()

